﻿//window.onload = initAll

function initAll() {
    //Stop overriding the buttons. by doing this everytime a link is clicked or button is pressed you get an error cause
    //this button does not exist on every page. Javascript natually handles events. create a onclick event on the button 
    //you want to check. DONT DO ONLOAD STUFF!

    //ButtonLogin = document.getElementById('ctl00_ContentPlaceHolderTopBar_ctl00_ButtonLogin');
    //ButtonLogin.onclick = validate;
}

function validate() {
    TextBoxUserEmail = document.getElementById('ctl00_ContentPlaceHolderTopBar_ctl00_TextBoxUserEmail');
    TextBoxPassword = document.getElementById('ctl00_ContentPlaceHolderTopBar_ctl00_TextBoxPassword');
    error_message = document.getElementById('error_message');
    validEmail = echeck(TextBoxUserEmail.value);
    validPassword = TextBoxPassword.value != "";

    error_message.style.color = 'red';
    if (!validEmail) {
        error_message.innerHTML = 'Invalid E-mail ID';
    }
    else if (!validPassword) {
        error_message.innerHTML = 'Please enter password';
    }
    return validEmail && validPassword;
}

function echeck(str) {
    var at = "@"
    var dot = "."
    var lat = str.indexOf(at)
    var lstr = str.length
    var ldot = str.indexOf(dot)
    if (str.indexOf(at) == -1) {
        return false
    }

    if (str.indexOf(at) == -1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) {
        return false
    }

    if (str.indexOf(dot) == -1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) {
        return false
    }

    if (str.indexOf(at, (lat + 1)) != -1) {
        return false
    }

    if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) {
        return false
    }

    if (str.indexOf(dot, (lat + 2)) == -1) {
        return false
    }
    if (str.indexOf(" ") != -1) {
        return false
    }

    return true
}